package cn.jluzh.blog.modules.comment.service.impl;

import cn.jluzh.blog.mapper.SysCommentMapper;
import cn.jluzh.blog.modules.article.entity.SysArticle;
import cn.jluzh.blog.modules.article.service.impl.ArticleServiceImpl;
import cn.jluzh.blog.modules.comment.dto.AuthorDto;
import cn.jluzh.blog.modules.comment.dto.ChildCommentDto;
import cn.jluzh.blog.modules.comment.entity.SysComment;
import cn.jluzh.blog.modules.comment.service.CommentService;
import cn.jluzh.blog.modules.comment.vo.SysCommentVo;
import cn.jluzh.blog.modules.user.entity.SysUser;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
  * @author: hjc
  **/

@Service
public class CommentServiceImpl extends ServiceImpl<SysCommentMapper, SysComment> implements CommentService {
    @Autowired
    SysCommentMapper sysCommentMapper;
    @Autowired
    ArticleServiceImpl articleService;

    @Override
    public List<SysCommentVo> queryArticleComments(Long id) {
        List<SysCommentVo> sysCommentVos = sysCommentMapper.queryArticleComments(id);
        return sysCommentVos;
    }

    @Override
    public List<SysCommentVo> queryChildrenComments(ChildCommentDto dto) {
        List<SysCommentVo> sysCommentVos = sysCommentMapper.queryChildrenComments(dto);
        return sysCommentVos;
    }

    @Override
    public JSONObject publishArticleComment(SysArticle sysArticle, SysUser sysUser, JSONObject json) {
        // 1、更新文章评论数+1
        sysArticle.setCommentNum(sysArticle.getCommentNum() + 1);
        articleService.updateById(sysArticle);

        // 2、插入一条文章评论
        SysComment comment = new SysComment();
        comment.setArticleId(sysArticle.getId());
        comment.setContent(json.getString("content"));
        comment.setCreateTime(new Date());
        comment.setUpdateTime(new Date());
        comment.setUserId(sysUser.getId());

        // 3、设置文章评论级别 level_flag
        //    为0:评论文章;1:评论某人评论;2:回复某人评论
        JSONObject parent = json.getJSONObject("parent");
        JSONObject toUser = json.getJSONObject("toUser");
        if (parent != null) {
            comment.setParentId(parent.getLong("id"));
            if (toUser != null) {
                comment.setLevelFlag("2");
                comment.setToUid(toUser.getInteger("id"));
            } else {
                comment.setLevelFlag("1");
            }
        } else {
            comment.setLevelFlag("0");
        }
        this.save(comment);

        // 4、返回评论数据
        JSONObject object = new JSONObject();
        object.put("id", comment.getId());
        object.put("level", comment.getLevelFlag());
        object.put("content", comment.getContent());
        object.put("createDate", comment.getCreateTime().getTime());
        object.put("author", new AuthorDto(comment.getUserId(), sysUser.getAvatarName(), sysUser.getNickname()));
        if (toUser != null) {
            object.put("toUser", toUser);
        }
        if (parent != null) {
            object.put("parent", parent);
        }

        return object;
    }

    @Override
    public boolean deleteComments(Long id) {
        return sysCommentMapper.deleteComments(id);
    }

    @Override
    public Integer getDeleteTotalById(Long id) {
        return sysCommentMapper.getDeleteTotalById(id);
    }
}
